package cn.itsource.pethome.pet.service.impl;

import cn.itsource.pethome.basic.domain.Point;
import cn.itsource.pethome.basic.query.BaseQuery;
import cn.itsource.pethome.basic.service.impl.BaseServiceImpl;
import cn.itsource.pethome.basic.util.DistanceUtil;
import cn.itsource.pethome.basic.util.PageList;
import cn.itsource.pethome.org.domain.Shop;
import cn.itsource.pethome.org.mapper.ShopMapper;
import cn.itsource.pethome.pet.domain.Pet;
import cn.itsource.pethome.pet.domain.PetDetail;
import cn.itsource.pethome.pet.domain.SearchMasterMessage;
import cn.itsource.pethome.pet.mapper.PetDetailMapper;
import cn.itsource.pethome.pet.mapper.PetMapper;
import cn.itsource.pethome.pet.mapper.SearchMasterMapper;
import cn.itsource.pethome.pet.service.ISearchMasterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @description: 描述:
 * @date: 2020/8/23 20:34
 * @author: summer
 * version: 1.0
 */
@Service
public class SearchMasterServiceImpl extends BaseServiceImpl<SearchMasterMessage> implements ISearchMasterService {

    @Autowired
    private PetMapper petMapper;

    @Autowired
    private PetDetailMapper petDetailMapper;

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private SearchMasterMapper searchMasterMapper;

    @Override
    public void publish(SearchMasterMessage masterMessage) {
        //把地址转为经纬度
        Point point = DistanceUtil.getPoint(masterMessage.getAddress());
        //把经纬度传递进去，返回最近的店铺
        Shop nearestShop = DistanceUtil.getNearestShop(point, shopMapper.findDepartment());
        //分配到最近的店铺
        masterMessage.setShop(nearestShop);
        //保存寻主信息
        searchMasterMapper.save(masterMessage);
    }

    @Override
    public void handleMessage(Pet pet) {
        //获取寻主信息id
        Long messageId = pet.getSearchMasterMessage().getId();
        //根据寻主信息id查询寻主对象
        SearchMasterMessage searchMasterMessage = searchMasterMapper.loadById(messageId);
        //标记寻主信息已被处理
        searchMasterMessage.setState(1);
        //修改数据
        searchMasterMapper.update(searchMasterMessage);

        //设置寻主信息
        pet.setSearchMasterMessage(searchMasterMessage);
        //保存宠物
        petMapper.save(pet);
        //保存宠物详情
        PetDetail petDetail = pet.getPetDetail();
        petDetail.setPet(pet);
        petDetailMapper.save(petDetail);//需要写sql语句
    }
}
